<?php

namespace App\Admin\Renders;

use Dcat\Admin\Grid;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CommonGrid
{
    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function player(Grid $grid): void
    {
        $grid->quickSearch(['name', 'slug'])
            ->placeholder('选手姓名/唯一标识');
        $grid->column('slug', '唯一标识');
        $grid->column('name', '选手姓名');
        $grid->column('polls', '当前票数');
        $grid->column('海报')
            ->if(fn() => $this->posters()->latest()->value('cover'))
            ->then(function (Grid\Column $column) {
                $column->display(fn() => sprintf('查看(%s)', $this->posters()->count()))
                    ->modal(function ($modal) {
                        $modal->title($this->name.' 海报预览');

                        return PlayerGuideRender::make()->payload(['player_id' => $this]);
                    });
            })->else(function (Grid\Column $column) {
                $column->display(function () {
                    return '未生成';
                });
            });

        $grid->column('点赞(人次/票数)')
            ->display(fn() => $this->likes_show)
            ->link(fn() => route('admin.players.likes.index', $this), '_self');
        $grid->column('评论(人次/票数)')
            ->display(fn() => $this->comments_show)
            ->link(fn() => route('admin.players.comments.index', $this), '_self');
        $grid->column('支持(人次/金额/票数)')
            ->display(fn() => $this->supports_show)
            ->link(fn() => route('admin.players.supports.index', $this), '_self');
        $grid->column('clicks', '浏览量');
        $grid->column('status')
            ->bool();
        $grid->column('created_at');
    }

    public static function comment(Grid $grid): void
    {
        $grid->column('user_id');
        $grid->column('user.nickname');
        $grid->column('content', '内容');
        $grid->column('polls', '票数');
        $grid->column('created_at');
    }

    public static function support(Grid $grid): void
    {
        $grid->column('user_id');
        $grid->column('user.nickname');
        $grid->column('gift.name', '礼物名称');
        $grid->column('qty', '数量');
        $grid->column('amount', '总金额');
        $grid->column('polls', '票数');
        $grid->column('created_at');
    }

    public static function like(Grid $grid): void
    {
        $grid->column('user_id');
        $grid->column('user.nickname');
        $grid->column('polls');
        $grid->column('created_at');
    }
}
